var Login = function () {

	var handleLogin = function() {
		$('.login-form').validate({
	            errorElement: 'span', //default input error message container
	            errorClass: 'help-block', // default input error message class
	            focusInvalid: false, // do not focus the last invalid input
	            rules: {
	                username: {
	                    required: true
	                },
	                password: {
	                    required: true
	                },
	                remember: {
	                    required: false
	                }
	            },

	            messages: {
	                username: {
	                    required: "Username is required."
	                },
	                password: {
	                    required: "Password is required."
	                }
	            },

	            invalidHandler: function (event, validator) { //display error alert on form submit   
	                $('.alert-error', $('.login-form')).show();
	            },

	            highlight: function (element) { // hightlight error inputs
	                $(element)
	                    .closest('.form-group').addClass('has-error'); // set error class to the control group
	            },

	            success: function (label) {
	                label.closest('.form-group').removeClass('has-error');
	                label.remove();
	            },

	            errorPlacement: function (error, element) {
	                error.insertAfter(element.closest('.input-icon'));
	            },

	            submitHandler: function (form) {
	           //     form.submit();
                    
                    var l_username = $("#username").val();
                    var l_password = $("#password").val();
                    var noloket = $("#noloket").val();
                    
                    
                    $.ajax({
                        url        : "c_login/process",
                        type       : "POST",
                        dataType   : "JSON",
                        data       : {
                                l_username:l_username,
                                l_password:l_password
                        },
                        success:function(response){
                            // console.log(response);
                            if (response != null){
                                if(response.message == "true"){
                                    window.location = "../"
                                }else{
                                    alert("Maaf, username atau password anda Salah !");
                                }
                            }else{
                                alert("Terjadi Kesalahan pada sistem!");
                            }
                        },
                        error : function(response){
                            // console.log(response);
                            alert("Terjadi Kesalahan pada sistem!");
                            
                        },
                    });
	            }
	        });
        
            $("#username").keypress(function(e){
				if(e.keyCode == 13){
					$("#password").focus();
				}
			});
			
			$("#password").keypress(function (e) {
	            if (e.which == 13) {
	                if ($('.login-form').validate().form()) {
	                    $('.login-form').submit();
	                }
	                return false;
	            }
	        });
        
	}
    
    return {
        //main function to initiate the module
        init: function () {
        	
            handleLogin();       
	       
        }

    };

}();